* Directories
// cd /Users/xiaosongw/Dropbox/Research/InformedSources/Replication/Analysis
// local datdir /Users/xiaosongw/Dropbox/Research/InformedSources/Replication/Build/Output/

cd /Users/byrned/Dropbox/Research/InformedSources/Replication/Analysis/
local datdir /Users/byrned/Dropbox/Research/InformedSources/Replication/Build/Output/

		
* -------------------------- Figure 8 Pricing Frequency -----------------------*

* Load data
use `datdir'is_mel_p_2005_2019, clear

* Sample period
keep if t>=td(1may2015) & t<=td(31dec2017)

* day of week month of year dummies
gen moy = month(t)
gen dow = dow(t)

* Coles off the platform
gen coles_off=cond(t>td(15mar2016),1,0)

bysort id (t): gen dc=c-c[_n-1]
gen dc0=dc if st_cyc_day==1
replace dc0=0 if dc0==.

* create lagged cost changes
gen pos_dc=dc if dc>0 & ~missing(dc)
replace pos_dc=0 if dc<=0
gen neg_dc=dc-pos_dc
forval i=1/7 {
	bysort id (t): gen pos_dc`i'=pos_dc[_n-`i']
	bysort id (t): gen neg_dc`i'=neg_dc[_n-`i']
}

* normalize cycle length
drop st_cyc_day_norm
keep if st_cyc_len > 6
gen st_cyc_day_norm = 0 if st_cyc_day == 0
forval i = 1/10	{
	replace st_cyc_day_norm = `i' if (st_cyc_day >= ((st_cyc_len - 1) * (`i' - 1) / 10 + 1)) & 	///
								   (st_cyc_day <= ((st_cyc_len - 1) * `i' / 10 + 1))
}
* drop last cycle day 
replace st_cyc_day_norm = . if st_cyc_day == st_cyc_len - 1

* dummies for all Coles vs rivals, pre/post case, by cycle decile (paper)
local nday = 10
forval j=1/2{
	if(`j'==1){
		forval k=0/`nday'{
			gen pre_coles_`k'=(1-coles_off)*cond(bid==3 & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)	/* Coles */
		}
		forval k=0/`nday'{
			gen post_coles_`k'=coles_off*cond(bid==3 & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)	/* Coles */
		}
	}	
	if(`j'==2){
		forval k=0/`nday'{
			gen pre_rival_`k'=(1-coles_off)*cond(bid!=3 & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)	/* Rivals */
		}	
		forval k=0/`nday'{
			gen post_rival_`k'=coles_off*cond(bid!=3 & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)	/* Coles */
		}		
	}
}

* dummies for all retailers, pre/post case, by cycle decile (appendix) 
forval j=1/6{
	forval k=0/`nday'{
		gen pre_`j'_`k'=(1-coles_off)*cond(bid==`j' & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)
	}	
	forval k=0/`nday'{
		gen post_`j'_`k'=coles_off*cond(bid==`j' & st_cyc_day_norm==`k' & st_cyc_id>0,1,0)
	}	
}
keep if bid>=1 & bid<=6


* save final data
sort id t
save Temp/regression_data, replace

gen is_cut = dp < 0

drop if t >= td(1mar2016) & t <= td(1may2016)
keep if bid < 6

* count the number of price cuts per station cycle and cycle decile
collapse (sum) is_cut (first) coles_off bid, by(id st_cyc_id st_cyc_day_norm)
gen coles = 1 if bid == 3
replace coles = 0 if bid ~= 3 & bid < 6

collapse (mean) is_cut (sd) sd_cut=is_cut (count) n=is_cut, by(coles coles_off st_cyc_day_norm) 
gen ub = is_cut + 1.96 * sd_cut / sqrt(n)
gen lb = is_cut - 1.96 * sd_cut / sqrt(n)

keep if st_cyc_day_norm >= 1 & st_cyc_day_norm <= 10
gen cyc_day = st_cyc_day_norm / 10

local y1=1.5
local y2=2.4
local x1=0.5
local x2=0.5
local xtext1=0.65
local ytext1=2.25
local panel1=0.04
local panel2=0.08

twoway 	(line is_cut cyc_day if coles_off==0&coles==1, lcolor(gs0*0.3) lpattern(solid) lwidth(medthick)) ///
		(line lb cyc_day if coles_off==0&coles==1, lcolor(gs0*0.3)lpattern(dash) lwidth(vthin)) ///
		(line ub cyc_day if coles_off==0&coles==1, lcolor(gs0*0.3) lpattern(dash) lwidth(vthin)) ///
		(line is_cut cyc_day if coles_off==1&coles==1, lcolor(gs0) lpattern(solid) lwidth(medthick)) ///
		(line lb cyc_day if coles_off==1&coles==1, lcolor(gs0) lpattern(dash) lwidth(vthin)) ///
		(line ub cyc_day if coles_off==1&coles==1, lcolor(gs0) lpattern(dash) lwidth(vthin) ///
		xtitle("Fraction of the Cycle Completed", height(0)) ytitle("Average Number of Price Cuts") ///
		xlabel(0 `" "0" "Restoration" "' .5 "1/2" 1 `" "1" "Full" "Cycle" "',labsize(*0.8)) ylabel(0.5(.5)3.5, grid gmin gmax) ///
		xscale(r(0, 1.01))	yscale(r(.3, 3.9)) ///
		plotregion(margin(zero) style(none) fcolor(white)) graphregion(color(white)) bgcolor(white) ///
		legend(ring(0) size(*0.7) position(1) symx(*0.5) region(lwidth(*0.05)) rows(2) order(1 2 4 5) ///
		label(1 "Coles {bf:ON}" "Informed Sources") ///
		label(4 "Coles {bf:OFF}" "Informed Sources") ///
		label(2 "95% CI") label(5 "95% CI"))) ///
		(pcarrowi `y2' `x2' `y1' `x1', color(black) lwidth(*1) barbsize(1) msize(2) ///
		text(`ytext1' `xtext1' "Coles price adjustment" "frequency decreases", justification(left) size(*.85)) ///
		text(3.75 `panel1' "{bf:A}", justification(left) size(*2)) ///
		text(3.75 `panel2' "        Coles", justification(left) size(*1.25)))
graph export Output/fig7a_freq_coles.pdf, as(pdf) replace	
		
local y1=2.55
local y2=1.9
local x1=0.5
local x2=0.5
local xtext1=0.5
local ytext1=1.75
local panel1=0.04			
local panel2=0.205			
			
twoway 	(line is_cut cyc_day if coles_off==0&coles==0, lcolor(gs0*0.3) lpattern(solid) lwidth(medthick)) ///
		(line lb cyc_day if coles_off==0&coles==0, lcolor(gs0*0.3) lpattern(dash) lwidth(vthin)) ///
		(line ub cyc_day if coles_off==0&coles==0, lcolor(gs0*0.3) lpattern(dash) lwidth(vthin)) ///
		(line is_cut cyc_day if coles_off==1&coles==0, lcolor(gs0) lpattern(solid) lwidth(medthick)) ///
		(line lb cyc_day if coles_off==1&coles==0, lcolor(gs0) lpattern(dash) lwidth(vthin)) ///
		(line ub cyc_day if coles_off==1&coles==0, lcolor(gs0) lpattern(dash) lwidth(vthin) ///
		xtitle("Fraction of the Cycle Completed", height(0)) ytitle("Average Number of Price Cuts") ///
		xlabel(0 `" "0" "Restoration" "' .5 "1/2" 1 `" "1" "Full" "Cycle" "',labsize(*0.8)) ylabel(0.5(.5)3.5, grid gmin gmax) ///
		xscale(r(0, 1.01))	yscale(r(.3, 3.9)) legend(off) ///
		plotregion(margin(zero) style(none) fcolor(white)) graphregion(color(white)) bgcolor(white) ///
		text(3.75 `panel1' "{bf:B}", justification(left) size(*2)) ///
		text(3.75 `panel2' "        Other major retailers", justification(left) size(*1.25)))
graph export Output/fig7b_freq_rival.pdf, as(pdf) replace



